#include "RawOverlay.h"

using namespace std;



ImageMatrix::ImageMatrix( QSize original )
        :osize(original),godown(false),coolsize(-1),rowsize(-1),roxmatrix(1.)
{
    if (qMax(osize.width(),osize.height()) > maxImageSide)
        godown = true;

    if (godown) {
        roxmatrix =  (qMax(osize.width(),osize.height()) * 100.000) / (maxImageSide * 100.000);
        imatrix.scale(roxmatrix,roxmatrix);
        matrix = imatrix.inverted();
    }

    qDebug() << "## -> roxmatrix  ai " << imatrix.m11();
    qDebug() << "## -> roxmatrix  bm " << matrix.m11();
    QSize full = matrix.mapRect(QRect(0,0,osize.width(),osize.height())).size();
    qDebug() << "## -> full " << full;
    request(full);
}


void ImageMatrix::rescale( const QRect rect  )
{
    if (qMax(rect.width(),rect.height()) > maxImageSide)
        return;
    const qreal actualscale = imatrix.m11();
    roxmatrix = (qMax(osize.width(),osize.height()) * 100.000) / (qMax(rect.width(),rect.height()) * 100.000);
    qDebug() << "## -> roxmatrix ->" << roxmatrix;
    QMatrix em;
    em.scale(roxmatrix,roxmatrix);
    imatrix = em;
    matrix = imatrix.inverted();
    QSize refull = matrix.mapRect(QRect(0,0,osize.width(),osize.height())).size();
    qDebug() << "## -> new refull  ->" << refull;
    if (!refull.isNull())
        request(refull);
}


void ImageMatrix::request( const QSize viewer , const  QPoint point )
{

    if (flatColumns) {
        delete  flatColumns;
    }
    if (flatRows)
        delete flatRows;

    coolsize = -1;
    rowsize = -1;
    qDebug() << "## -> rebuild array  ->" << viewer;
    smallsize = viewer;
    const uint maxrequest = qMax(viewer.width(),viewer.height());
    const uint maxenabler = qMax(osize.width(),osize.height());

    id = maxrequest + maxenabler;
    int jumps = 1;
    if (jumps == 0)
        jumps =1;
    double cur_col = point.x() > 0 ? point.x() : 0;     /* init coumn */
    double cur_row = point.y() > 0 ? point.y() : 0;     /* init coumn */
    flatColumns = new int[viewer.width()];
    flatRows = new int[viewer.height()];

    for ( register int col = 0; col < viewer.width(); col++ )
    {
        coolsize++;
        flatColumns[col] = static_cast<int>( cur_col );
        cur_col += imatrix.m11();
    }

    for ( register int row = 0; row < viewer.height(); row++ )
    {
        rowsize++;
        flatRows[row] = static_cast<int>( cur_row );
        cur_row += imatrix.m11();
    }
}



void ImageMatrix::print()
{
    cout << "--------------------------- cool print array start -----------------------------" << endl;
    for (int i=0; i<csize(); i++)
        cout << flatColumns[i] << "|";
    cout << endl;
    cout << "--------------------------- cool  print array stop -----------------------------" << endl;

    cout << "--------------------------- rows print array start -----------------------------" << endl;
    for (int i=0; i<rsize(); i++)
        cout << flatRows[i] << "|";
    cout << endl;
    cout << "--------------------------- rows  print array stop -----------------------------" << endl;

    qDebug() << "## -> rsize() " << rsize();
    qDebug() << "## -> csize() " << csize();
    qDebug() << "## -> smallsize " << smallsize;



}



ImageMatrix::~ImageMatrix()
{
    if (flatColumns)
        delete [] flatColumns;
    if (flatRows)
        delete [] flatRows;

}



qreal weightMega( const qint64 peso  )
{
    QString humanread;
    double canno = peso / 1024;
    int sale = canno;
    if (peso > 0) {
        if (canno < 1) {
            sale = 1;
        }
    }
    if (sale < 1025) {
        humanread = QString("0.%1").arg(sale);
        return humanread.toDouble();
    }
    float megad = sale / 1024;
    return megad;
}





const QVector<QRgb>& colorTable(RawOverlay::ColorType color)
{
    static QVector<QRgb> Gray8MinIsWhite;
    static QVector<QRgb> Gray8MinIsBlack;
    static QVector<QRgb> BilevelMinIsBlack;
    static QVector<QRgb> BilevelMinIsWhite;
    static QVector<QRgb> Empty;

    static bool firstrun=true;
    if (firstrun)
    {
        firstrun=false;
        Gray8MinIsBlack.resize(256);
        for (int i=0;i<256;i++)
            Gray8MinIsBlack[i]=qRgb(i, i, i);

        Gray8MinIsWhite.resize(256);
        for (int i=0;i<256;i++)
            Gray8MinIsWhite[i]=qRgb(255-i, 255-i, 255-i);

        BilevelMinIsBlack.resize(2);
        BilevelMinIsBlack[0]=qRgb(0,0,0);
        BilevelMinIsBlack[1]=qRgb(255,255,255);

        BilevelMinIsWhite.resize(2);
        BilevelMinIsWhite[0]=qRgb(255,255,255);
        BilevelMinIsWhite[1]=qRgb(0,0,0);
    }
    switch (color)
    {
    case RawOverlay::BilevelMinIsBlack :
        return BilevelMinIsBlack;
    case RawOverlay::BilevelMinIsWhite :
        return BilevelMinIsWhite;
    case RawOverlay::Gray8MinIsBlack :
        return Gray8MinIsBlack;
    case RawOverlay::Gray8MinIsWhite :
        return Gray8MinIsWhite;
    default:
        return Empty;
    }
}





